﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MotionCS.Controller
{
    public class Smc300Di : IDiBit
    {
        /// <summary>
        /// 标识符
        /// </summary>
        public int Identity { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 卡号
        /// </summary>
        private ushort cardno;
        /// <summary>
        /// 有效电平
        /// </summary>
        private int logic;
        /// <summary>
        /// 输入口
        /// </summary>
        private ushort index;
        public ushort Index 
        { 
            get => index;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idx"></param>
        public Smc300Di(int id, ushort idx)
        {
            this.Identity = id;
            logic = idx / 1000;
            index = (ushort)(idx % 1000);
        }
        /// <summary>
        /// 是否导通
        /// </summary>
        /// <returns></returns>
        public bool IsOn()
        {
            short value = Smc300Api.ReadInbit(index);

            return logic == 0 ? value == 0: value == 1;
        }
        /// <summary>
        /// 是否断开
        /// </summary>
        /// <returns></returns>
        public bool IsOff()
        {
            short value = Smc300Api.ReadInbit(index);
            return logic == 0 ? value == 1 : value == 0;
        }
        /// <summary>
        /// 输入值
        /// </summary>
        /// <returns></returns>
        public Int32 Inbit()
        {
            short value = Smc300Api.ReadInbit(index);
            return (Int32)value;
        }
    }

    public class Smc300Do : IDoBit
    {
        /// <summary>
        /// 标识符
        /// </summary>
        public int Identity { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 有效电平
        /// </summary>
        private int logic;
        /// <summary>
        /// 卡号
        /// </summary>
        private ushort cardno;
        /// <summary>
        /// 输出号
        /// </summary>
        private ushort index;
        public ushort Index
        {
            get => index;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idx"></param>
        public Smc300Do(int id, ushort idx)
        {
            this.Identity = id;
            logic = idx / 1000;
            int bit = idx % 1000;
            index = (ushort)(bit % 16);
            cardno = (ushort)(bit / 16);
        }
        /// <summary>
        /// 输出导通
        /// </summary>
        public void TurnOn()
        {
            ushort val = (ushort)(logic == 0 ? 0 : 1);
            Smc300Api.WriteOutbit(index, val);
        }
        /// <summary>
        /// 输出断开
        /// </summary>
        public void TurnOff()
        {
            ushort val = (ushort)(logic == 0 ? 1 : 0);
            Smc300Api.WriteOutbit(index, val);
        }
        /// <summary>
        /// 输出值
        /// </summary>
        /// <param name="state"></param>
        public void SetOut(uint dat)
        {
            int val = dat == 0 ? logic == 0 ? 1 : 0 : logic == 0 ? 0 : 1;
            Smc300Api.WriteOutbit(index, (ushort)val);
        }
        /// <summary>
        /// 是否导通
        /// </summary>
        public bool IsTurnOn()
        {
            short value = Smc300Api.ReadOutbit(index);

            return logic == 0 ? value == 0: value == 1;
        }
    }
}
